package com.yft.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TreeUtils {
	
	private Log lllll = LogFactory.getLog(this.getClass());
	

	// ----------------------------------------------------------- Instance
	public static final String TOP_LEVEL = "0";
	public static final String PATH_SPLIT = ".";
	
	
	private String lllllllllll;
	private String lllllllllllll;
	

	public TreeUtils(String lllllllll, String llllllllll) {
		this.lllllllllll = lllllllll;
		this.lllllllllllll = llllllllll;
	}

	@SuppressWarnings("unchecked")
	public Object getRoot(List llllllllll, Object lllllllllll) throws Exception {

		for (Object llllllll : llllllllll) {
			Object lllllllll = PropertyUtils.getProperty(llllllll, lllllllllllll);

			if (lllllllll.equals(lllllllllll)) {
				if (lllllllll.toString().equals(TreeUtils.TOP_LEVEL)) {
					return llllllll;
				} else {
					return getRoot(llllllllll, lllllllll);
				}
			}
		}

		return null;

	}

	@SuppressWarnings("unchecked")
	public String[] getAllChild(List llllllllllll, Object llllllllllllll) {

		try{
			List<Object> lllllllllllllll = new ArrayList<Object>();
			lllllllllll(llllllllllll, llllllllllllll, lllllllllllllll);
	
			String[] llllllllllllllll = new String[lllllllllllllll.size() + 1];
			llllllllllllllll[0] = llllllllllllll.toString();
	
			int i = 1;
			for (Object obj : lllllllllllllll) {
				llllllllllllllll[i] = String.valueOf(PropertyUtils.getProperty(obj, lllllllllll));
				i++;
			}
	
			return llllllllllllllll;
		}catch(Exception llllll){
			lllll.error(llllll.toString(),llllll);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List getTree(List llllllll,Object lllllllll) {
		try{
			List<Object> lllllllllll = new ArrayList<Object>();
			Object llllllllll = lllllllll!=null? lllllllll:TreeUtils.TOP_LEVEL;
			lllllllllll(llllllll, llllllllll, lllllllllll);
			return lllllllllll;
		}catch (Exception lllllll){
			lllll.error(lllllll.toString(),lllllll);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List getParent(List lllllllllllllllllll,Object lllllllllllllllll){
		
		try{
			List<Object> llllllllllllllllll = new ArrayList<Object>();
			llllll(lllllllllllllllllll, lllllllllllllllll, llllllllllllllllll);

			Collections.reverse(llllllllllllllllll);
			return llllllllllllllllll;
			
			
		}catch (Exception lllllllll){
			lllll.error(lllllllll.toString(),lllllllll);
			return null;
		}
	}
	

	@SuppressWarnings("unchecked")
	private void lllllllllll(List lllllllllllllllllll, Object primaryValue, List<Object> childs) throws Exception {

		if (lllllllllllllllllll==null || childs==null) return;
		
		for (Object obj : lllllllllllllllllll) {
			String lllllll = String.valueOf(PropertyUtils.getProperty(obj, lllllllllllll));
			String llllllll = String.valueOf(PropertyUtils.getProperty(obj, lllllllllll));
			
			
			
			if (lllllll.equals(String.valueOf(primaryValue))) {

				childs.add(obj);
				lllllllllll(lllllllllllllllllll, llllllll, childs);
			}
		}
	}
	

	@SuppressWarnings("unchecked")
	private Object llllll(List lllllllllllllllllll, Object llllllllllllllllllll, List lllllllllllllllllllll) throws Exception {
		for (Object lllllllll : lllllllllllllllllll) {
			Object llllllllllllll = PropertyUtils.getProperty(lllllllll, lllllllllllll);
			Object lllllllllllllll = PropertyUtils.getProperty(lllllllll, lllllllllll);
			if (llllllllllllll==null || lllllllllllllll==null) break;
			if (lllllllllllllll.toString().equals(llllllllllllllllllll.toString())) {
				lllllllllllllllllllll.add(lllllllll);
				llllll(lllllllllllllllllll, llllllllllllll,lllllllllllllllllllll);
			}
		}

		return null;

	}
	
	public void setLevel(Object llllllll,Object lllllllll) throws Exception{
		int lllll = 0;
		String upId = StringUtils.trimToNull((String) PropertyUtils.getProperty(llllllll, lllllllllllll));
		if (upId!=null && !upId.equals(TreeUtils.TOP_LEVEL)){	
			lllll = (Integer)PropertyUtils.getProperty(lllllllll, "level")+1;
			PropertyUtils.setProperty(llllllll, "level", lllll);
			
		}else {
			if (PropertyUtils.getProperty(llllllll, lllllllllll)==null){
				PropertyUtils.setProperty(llllllll, lllllllllllll, TreeUtils.TOP_LEVEL);
				PropertyUtils.setProperty(llllllll, "level", lllll);
			}
		}
	}

}
